<?php
/**
 * 备份管理脚本
 * 用于列出、压缩和清理备份文件
 */

// 获取所有备份文件
$backupFiles = glob("oa_backup_*.sql");
$compressedFiles = glob("oa_backup_*.sql.gz");

echo "========================================\n";
echo "数据库备份管理\n";
echo "========================================\n\n";

echo "SQL备份文件:\n";
$totalSize = 0;
foreach ($backupFiles as $file) {
    $size = filesize($file);
    $totalSize += $size;
    echo "- " . $file . " (" . formatBytes($size) . ")\n";
}

echo "\n压缩备份文件:\n";
foreach ($compressedFiles as $file) {
    $size = filesize($file);
    $totalSize += $size;
    echo "- " . $file . " (" . formatBytes($size) . ")\n";
}

echo "\n总计: " . (count($backupFiles) + count($compressedFiles)) . " 个文件, " . formatBytes($totalSize) . "\n\n";

// 提供操作选项
echo "可用操作:\n";
echo "1. 压缩所有SQL备份文件\n";
echo "2. 删除所有备份文件\n";
echo "3. 显示最新备份文件信息\n";
echo "0. 退出\n\n";

echo "请输入您的选择: ";
$handle = fopen("php://stdin", "r");
$choice = trim(fgets($handle));
fclose($handle);

switch ($choice) {
    case '1':
        compressBackups($backupFiles);
        break;
    case '2':
        deleteBackups($backupFiles, $compressedFiles);
        break;
    case '3':
        showLatestBackup($backupFiles, $compressedFiles);
        break;
    case '0':
        echo "退出程序\n";
        break;
    default:
        echo "无效选择\n";
}

/**
 * 压缩备份文件
 */
function compressBackups($files) {
    if (empty($files)) {
        echo "没有找到需要压缩的备份文件\n";
        return;
    }
    
    echo "开始压缩备份文件...\n";
    foreach ($files as $file) {
        echo "正在压缩: $file\n";
        $compressedFile = $file . '.gz';
        
        // 读取原文件内容
        $content = file_get_contents($file);
        
        // 压缩并写入新文件
        $compressed = gzencode($content, 9);
        file_put_contents($compressedFile, $compressed);
        
        // 删除原文件
        unlink($file);
        
        echo "已创建: $compressedFile (" . formatBytes(filesize($compressedFile)) . ")\n";
    }
    echo "压缩完成\n";
}

/**
 * 删除备份文件
 */
function deleteBackups($sqlFiles, $gzFiles) {
    echo "警告: 此操作将删除所有备份文件，是否继续? (y/N): ";
    $handle = fopen("php://stdin", "r");
    $confirm = trim(fgets($handle));
    fclose($handle);
    
    if (strtolower($confirm) !== 'y') {
        echo "操作已取消\n";
        return;
    }
    
    $deleted = 0;
    foreach ($sqlFiles as $file) {
        if (unlink($file)) {
            echo "已删除: $file\n";
            $deleted++;
        }
    }
    
    foreach ($gzFiles as $file) {
        if (unlink($file)) {
            echo "已删除: $file\n";
            $deleted++;
        }
    }
    
    echo "总共删除了 $deleted 个文件\n";
}

/**
 * 显示最新备份文件信息
 */
function showLatestBackup($sqlFiles, $gzFiles) {
    $allFiles = array_merge($sqlFiles, $gzFiles);
    if (empty($allFiles)) {
        echo "没有找到备份文件\n";
        return;
    }
    
    // 按修改时间排序
    usort($allFiles, function($a, $b) {
        return filemtime($b) - filemtime($a);
    });
    
    $latestFile = $allFiles[0];
    $size = filesize($latestFile);
    $modified = date('Y-m-d H:i:s', filemtime($latestFile));
    
    echo "最新备份文件:\n";
    echo "- 文件名: $latestFile\n";
    echo "- 文件大小: " . formatBytes($size) . "\n";
    echo "- 修改时间: $modified\n";
}

/**
 * 格式化字节大小
 */
function formatBytes($size, $precision = 2) {
    $units = array('B', 'KB', 'MB', 'GB', 'TB');
    
    for ($i = 0; $size > 1024 && $i < count($units) - 1; $i++) {
        $size /= 1024;
    }
    
    return round($size, $precision) . ' ' . $units[$i];
}

